/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package org.latestbit.smartobjects.perftests;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.concurrent.atomic.AtomicInteger;

import org.latestbit.smartobjects.*;
import org.latestbit.smartobjects.exceptions.SmartBusSystemException;
import org.latestbit.smartobjects.network.EndpointLocation;
import org.latestbit.smartobjects.network.IHandshakeManager;
import org.latestbit.smartobjects.network.IHandshakeManagerListener;
import org.latestbit.smartobjects.testmodel.*;

public class SimpleServer implements ISimpleInterfaceSyncSmartObject, Runnable {

	ISmartBus bus = new SmartBus();
	static String location = "tcp://0.0.0.0:7777";
	SimpleInterfaceSyncSkeleton skeleton = new SimpleInterfaceSyncSkeleton (
			this
	); 
	AtomicInteger tps = new AtomicInteger(0);
	boolean stopCalcFlag = false;
	static boolean fullThreadModel = false;
	
		
	public static void main(String[] args) throws Exception {
		if(args.length > 0)
			location = args[0];
		if(args.length > 1)
			fullThreadModel = true;
		new SimpleServer().start();
		System.out.println("Exiting...");
	}

	private void start() throws Exception {
		bus.getHandshakeManagerFactory().setHandshakeManagerListener(new IHandshakeManagerListener() {

			@Override
			public void onHandshakeBegin(IHandshakeManager manager) {
				System.out.println("Handshake begin from " + manager.getConnection().getEndpointURI() +"...");
			}

			@Override
			public void onHandshakeComplete(IHandshakeManager manager) {
				System.out.println("Handshake complete.");	
			}

			@Override
			public void onHandshakeError(IHandshakeManager manager, Exception ex) {
				System.err.println("Handshake error: "+ex);
			}
			
		});
		
		BufferedReader input = new BufferedReader ( new InputStreamReader ( System.in ) );
		
		System.out.println("SimpleServer is loading...");
		
		if(fullThreadModel) {
			skeleton.setThreadModel(IServerSkeleton.ThreadModel.FULL_MULTI_THREAD_MODEL);
			System.out.println("Server is loading in fully multithreaded model.");
		}
		skeleton.publish(bus, new EndpointLocation ( bus, location) );
		Thread calcThread = new Thread(this);
		calcThread.setDaemon(true);
		
		calcThread.start();
		System.out.println("SimpleServer is ready. Please enter to exit");
		input.readLine();
		System.out.println("SimpleServer is unloading...");
		stopCalcFlag = true;
		calcThread.join();
		System.out.println("Destroying objects...");
		// skeleton.destroy();
		// bus.destroy();

		// System.exit(0);
	}
	

	@Override
	public void run() {
		while(!stopCalcFlag) {
			try {
				Thread.sleep(1000);
				System.out.println("TPS: "+tps.getAndSet(0));
			} catch (InterruptedException e) {
				e.printStackTrace();
				break;
			}
		}
	}	

	@Override
	public String helloWorld(String name)
			throws SmartBusSystemException {
		tps.incrementAndGet();
		return "Hello "+name;
	}

	@Override
	public String helloWorld2(String name, ISimpleStructure firstStructure) throws SmartBusSystemException {	return null; }

	@Override
	public ISimpleStructure helloWorld3(String name, ISimpleStructure firstStructure) throws SmartBusSystemException { return null; }

	@Override
	public String helloWorld4(String name) throws SmartBusSystemException, SimpleException {	return null; }

	@Override
	public void manualDestroy() 
		throws SmartBusSystemException {}

	@Override
	public String helloWorldEncrypted(String name)
			throws SmartBusSystemException {
		return helloWorld ( name );
	}
	
	@Override
	public void destroy() {
		
	}



}
